package com.example.administrator.orchardeveryday.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.administrator.orchardeveryday.R;
import com.example.administrator.orchardeveryday.adapter.KindFragmentContentAdapter;
import com.example.administrator.orchardeveryday.bean.Common;
import com.example.administrator.orchardeveryday.bean.KindEntity;
import com.example.administrator.orchardeveryday.bean.SubLevel;
import com.example.administrator.orchardeveryday.config.UrlConstants;
import com.example.administrator.orchardeveryday.widget.GridViewForScrollView;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

/**
 * A simple {@link Fragment} subclass.
 */
public class KindFragment extends Fragment {

    private ListView lv;
    private GridViewForScrollView gv;
    private TextView tv;
    private AsyncHttpClient httpClient = new AsyncHttpClient();
    private ArrayList<Common> commons=new ArrayList<>();
    private ArrayList<String> list=new ArrayList<>();
    private ArrayAdapter<String> adapter;
    private ArrayList<SubLevel> sublevels=new ArrayList<>();
    private KindFragmentContentAdapter contentAdapter;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_kind, container, false);
        initView(view);
        loadData();
        return view;
    }
    private void initView(View view) {
        lv= (ListView) view.findViewById(R.id.fragment_kind_list_lv);
        gv= (GridViewForScrollView) view.findViewById(R.id.fragment_kind_content_gv);
        tv= (TextView) view.findViewById(R.id.fragment_kind_content_title);
        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                sublevels.clear();
                sublevels.addAll(commons.get(position).getSub_level());
                contentAdapter.notifyDataSetChanged();
            }
        });
        adapter=new ArrayAdapter<>(getActivity(),android.R.layout.simple_list_item_1,list);
        lv.setAdapter(adapter);
        contentAdapter=new KindFragmentContentAdapter(sublevels,getActivity());
        gv.setAdapter(contentAdapter);
    }

    private void loadData() {
        httpClient.post(UrlConstants.URL_KIND, getRequestParams(), new TextHttpResponseHandler() {
            @Override
            public void onFailure(int i, Header[] headers, String s, Throwable throwable) {

            }

            @Override
            public void onSuccess(int i, Header[] headers, String s) {
                KindEntity entity = JSONObject.parseObject(s, KindEntity.class);
                if (entity != null) {
                    commons.addAll(entity.getCommon());
                }
                for (int j = 0; j < commons.size(); j++) {
                    list.add(commons.get(j).getName());
                }
                adapter.notifyDataSetChanged();
                sublevels.addAll(commons.get(0).getSub_level());
                contentAdapter.notifyDataSetChanged();
            }
        });
    }


    private RequestParams getRequestParams() {
        RequestParams params = new RequestParams();
        params.add("sign", "c316fe3aee275e34712eea2cdaa92d70");
        params.add("timestamp", "1438873421");
        params.add("platform", "ANDROID");
        params.add("source", "app");
        params.add("connect_id", "");
        params.add("region_id", "143949");
        params.add("service", "product.getCatList");
        params.add("device_id", "93789e8978f2eefdcf52006b4d96ee23");
        params.add("channel", "qq");
        params.add("version", "2.3.0");
        return params;
    }
}
